//
//  FlowCenterVC.swift
//  Employee
//
//  Created by swordjoy on 2017/6/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Moya

class FlowCenterVC: BaseVC {

  fileprivate var applyVC = FlowListVC()
  fileprivate var checkVC = FlowListVC()
  fileprivate var copyVC = FlowListVC()
  
  fileprivate var pullMenu: PullMenuView!
  fileprivate var ant: SJAnt!
  fileprivate var searchKey = FlowListSend()
  fileprivate var cancle: Cancellable?
  fileprivate var currentIndex: Int = -1
  
  override func viewDidLoad() {
    super.viewDidLoad()
    create()
    pullData()
  }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        GlobalConfig.baiduStatisticsPageViewStart("流程审批")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        GlobalConfig.baiduStatisticsPageViewEnd("流程审批")
    }
    
  
  deinit {
    cancle?.cancel()
  }
}

// MARK: - action

extension FlowCenterVC {
  @objc fileprivate func newFlowNext() {
    let vc = FlowVC()
    vc.refreshBefore = { self.update(with: 0) }
    navigationController?.pushViewController(vc, animated: true)
  }
}

extension FlowCenterVC: SJAntDelegate {
  
  func SJAntIndexChange(index: Int) {
    if index == currentIndex { return }
    
    currentIndex = index
    pullMenu.updateStates(currentIndex)
    switch index {
    case 0: searchKey.status = ""
    case 1: searchKey.status = ""
    case 2: searchKey.status = ""
    default:
        fatalError()
    }
    searchKey.flowId = ""
    update(with: index)
  }
  
  fileprivate func update(with index: Int) {
    
    switch index {
    case 0:
      self.applyVC.pullData(with: searchKey)
    case 1:
      self.checkVC.pullData(with: searchKey)
    case 2:
      self.copyVC.pullData(with: searchKey)
    default: break
      
    }
  }
  
}

// MARK: - 

extension FlowCenterVC: PullMenuViewDelegate {
  func pullMenuTypeChange(typeId: String) {
    searchKey.flowId = typeId
    update(with: currentIndex)
  }
  
  func pullMenuStateChange(index: PullMenuStateEnum) {
    switch index {
    case .all:
      searchKey.status = ""
    case .checking:
      searchKey.status = "Auditing"
    case .pass:
      searchKey.status = "NotPassed"
    case .finish:
      searchKey.status = "Finished"
    case .recall:
      searchKey.status = "Revoked"
    }
    update(with: currentIndex)
  }
    
    func pullMenuDefault() {
        searchKey.status = "Auditing"
        update(with: currentIndex)
    }
}

// MARK: - fileprivate

extension FlowCenterVC {
  
  fileprivate func pullData() {
    cancle = FlowRequest.shared.getFlowType(success: { (datas) in
      let flow = Flow(id: "", name: "全部类型")
      var temps = datas
      temps.insert(flow, at: 0)
      self.pullMenu.flows = temps
    }, failure: {
      SJApp.log("获取类型失败")
    })
  }
  
  fileprivate func create() {
    title = "流程审批"
    addBackItem()
    
    navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "func_newflow_blue")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(newFlowNext))

    
    pullMenu = PullMenuView(frame: CGRect(x: 0, y: 44, width: UIScreen.width, height: 44))
    pullMenu.delegate = self
    
    applyVC.title = "我的申请"
    applyVC.parentVC = self
    applyVC.type = .apply
    
    checkVC.title = "我的审批"
    checkVC.parentVC = self
    checkVC.type = .check
    
    copyVC.title = "抄送我的"
    copyVC.parentVC = self
    copyVC.type = .copy

    searchKey.status = ""
    
    let VCs = [applyVC, checkVC, copyVC]
    
    let frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noNavBarHeight)
    ant = SJAnt(frame: frame, VCs: VCs as [UIViewController], parentVC: self, delegate: self)
    ant.backgroundColor = UIColor.white
    
    view.addSubview(ant)
    view.addSubview(pullMenu)
  }
}


